<!-- 17 -->
<script lang='ts' setup>
// @ts-nocheck
import { useRoute } from 'vue-router'
import { changeState, getDetailEx } from '@/api/request'
import { useGlobalStore } from '@/store/GlobalStore/index'
const store=useGlobalStore()
const route = useRoute()
const router=useRouter()
const name=store.userInfo.username
const rid = ref('')
const tid = ref('')
const sid = route.query.sid
const cid=store.userInfo.cid
const ws = new WebSocket(`ws://210.44.37.172:8082/message?id=${sid}&role=student`)
ws.onopen = function (e) {
	console.log('连接成功')
	// console.log('断开之前')
	// setTimeout(() => {
	// 	ws.close()
	// }, 3000)
	// console.log('断开了')
	// ws.send(
	// 	// '{"rid":"2002","tid":"3000","kid":"3000","value":"3.33","timeStamp":"3.2.3"}'
	// 	`{"role":"student","sourceId":"${sid}","eid":"3","rid":"${rid.value}","destId":"${tid.value}","kid":"k3","value":"3.33","timeStamp":"3.2.3"}`
	// )
}
ws.onmessage = function (e) {
	console.log(e.data, '接收返回的消息')
}
ws.onclose = function () {
	console.log('连接关闭')
}
ws.onerror = function () {
	console.log('连接出错')
}

onMounted(() => {
	console.log('组件挂载')

	rid.value = route.query.id
	tid.value = route.query.tid
	console.log('sid', sid, 'rid', rid.value, 'tid', tid.value)
	const jwtToken = localStorage.getItem('jwtToken')
	// 拿到保存的实验数据
	getDetailEx(jwtToken, 17, rid.value).then((res) => {
		console.log(res.data.data[0], 'socket数据')
		model.value.forEach((element) => {
			var obj = element
			var kid = obj.kid
			if (res.data.data[0].hasOwnProperty(kid)) {
				obj.value = res.data.data[0][kid]
				obj.timeStamp = res.data.data[0]['t' + kid]
			}
		})
	})
})
// 填每一个空的时间
const fillTime = ref([])
// 失去焦点时触发事件记录时间
const getTime = function (index) {
	model.value[index].timeStamp = new Date().toLocaleString()
	// 处理model数组的数据
	model.value = model.value.map((item) => ({
		role: 'student',
		sourceId: sid,
		rid: rid.value,
		destId: tid.value,
		eid: '17',
		kid: item.kid,
		// 返回这个对象中第一个属性的value值
		value: item.value,
		timeStamp: item.timeStamp,
	}))
	console.log('输出的', model.value)
	ws.send(
		// '{"rid":"2002","tid":"3000","kid":"3000","value":"3.33","timeStamp":"666666666"}'
		`${JSON.stringify(model.value[index])}`
	)
	console.log(model.value[index], '填的空')
}
// 提交
const apply = function () {
	// console.log('提交')
	// storage()
	model.value = model.value.map((item) => ({
		rid: rid.value,
		tid: tid.value,
		kid: Object.keys(item)[0],
		// 返回这个对象中第一个属性的value值
		value: item[Object.keys(item)[0]],

		timeStamp: item.timeStamp,
	}))
	changeState(jwtToken, rid, 1).then((res) => {
		console.log(res)
	})
	console.log(model.value, '提交成功')
	ElMessage.success('提交成功')
}
const model = ref([
    { kid: 'k0', value: '', timeStamp: '' },
    { kid: 'k1', value: '', timeStamp: '' },
    { kid: 'k2', value: '', timeStamp: '' },
    { kid: 'k3', value: '', timeStamp: '' },
    { kid: 'k4', value: '', timeStamp: '' },
    { kid: 'k5', value: '', timeStamp: '' },
    { kid: 'k6', value: '', timeStamp: '' },
    { kid: 'k7', value: '', timeStamp: '' },
    { kid: 'k8', value: '', timeStamp: '' },
    { kid: 'k9', value: '', timeStamp: '' },
    { kid: 'k10', value: '', timeStamp: '' },
    { kid: 'k11', value: '', timeStamp: '' },
    { kid: 'k12', value: '', timeStamp: '' },
    { kid: 'k13', value: '', timeStamp: '' },
    { kid: 'k14', value: '', timeStamp: '' },
    { kid: 'k15', value: '', timeStamp: '' },
    { kid: 'k16', value: '', timeStamp: '' },
    { kid: 'k17', value: '', timeStamp: '' },
    { kid: 'k18', value: '', timeStamp: '' },
    { kid: 'k19', value: '', timeStamp: '' },
])


</script>
<template>
    <div class="paper-wrap">
        <n-card>
            <!-- 学生信息 -->
            <div class="paper-title">
                <h2>麦克尔逊干涉仪的调节与使用</h2>
                <div class="stu-info">
                    <div class="stu-info-item">
                        <span>学号：</span>
                        <span>{{sid}}</span>
                    </div>
                    <div class="stu-info-item">
                        <span>姓名：</span>
                        <span>{{name}}</span>
                    </div>
                    <div class="stu-info-item">
                        <!-- <span>班级：</span>
                        <span>{{cid}}</span> -->
                    </div>
                </div>
            </div>
            <!-- 实验的基本信息 -->
            <n-divider title-placement="left">
                <h2>一、实验目的</h2>
            </n-divider>
            <div class="paper-purpose">
                <ol>
                    <li>
                        <el-input v-model="model[0].value" placeholder="第一条" @blur="getTime(0)"></el-input>
                    </li>
                    <li> <el-input v-model="model[1].value" placeholder="第二条" @blur="getTime(1)"></el-input> </li>
                </ol>
            </div>
            <n-divider title-placement="left">
                <h2>二、实验仪器</h2>
            </n-divider>
            <div class="paper-instrument">
                <el-input v-model="model[2].value" placeholder="请填写需要的实验仪器" @blur="getTime(2)"></el-input>
            </div>
            <n-divider title-placement="left">
                <h2>三、实验原理</h2>
            </n-divider>
            <div class="paper-principle">
                <p class="principles"> 写出实验基本原理级公式，并画出原理图 </p>
            </div>

            <n-divider title-placement="left">
                <h2>四、数据记录与处理</h2>
            </n-divider>
            <h3>（一）基本量测量</h3>
            <div class="paper-data">
                <!-- 第一个表单 -->
                <h3> 牛顿环测量透镜的曲率半径</h3>
                <table border="1" cellpadding="2" style="
						width: 100%;
						border-collapse: collapse;
						border: 2px solid #000;
					" class="ntable">
                    <thead>
                        <tr>
                            <th>测量次数</th>
                            <th>干涉圆环编号</th>
                            <th>d<sub>m</sub>/mm</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td class="w-50 m-2">1</td>
                            <td class="w-50 m-2">0</td>
                            <td><el-input v-model="model[3].value" class="w-50 m-2" @blur="getTime(3)" /></td>
                        </tr>
                        <tr>
                            <td class="w-50 m-2">2</td>
                            <td class="w-50 m-2">30</td>
                            <td><el-input v-model="model[4].value" class="w-50 m-2" @blur="getTime(4)" /></td>
                        </tr>
                        <tr>
                            <td class="w-50 m-2">3</td>
                            <td class="w-50 m-2">60</td>
                            <td><el-input v-model="model[5].value" class="w-50 m-2" @blur="getTime(5)" /></td>
                        </tr>
                        <tr>
                            <td class="w-50 m-2">4</td> 
                            <td class="w-50 m-2">90</td>
                            <td><el-input v-model="model[6].value" class="w-50 m-2" @blur="getTime(6)" /></td>
                        </tr>
                        <tr>
                            <td class="w-50 m-2">5</td>
                            <td class="w-50 m-2">120</td>
                            <td><el-input v-model="model[7].value" class="w-50 m-2" @blur="getTime(7)" /></td>
                        </tr>
                        <tr>
                            <td class="w-50 m-2">6</td>
                            <td class="w-50 m-2">150</td>
                            <td><el-input v-model="model[8].value" class="w-50 m-2" @blur="getTime(8)" /></td>
                        </tr>
                        <tr>
                            <td class="w-50 m-2">7</td>
                            <td class="w-50 m-2">180</td>
                            <td><el-input v-model="model[9].value" class="w-50 m-2" @blur="getTime(9)" /></td>
                        </tr>
                        <tr>
                            <td class="w-50 m-2">8</td>
                            <td class="w-50 m-2">210</td>
                            <td><el-input v-model="model[10].value" class="w-50 m-2" @blur="getTime(10)" /></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <!-- 第二个表单 -->
            <h3>(二)用线性回归法处理实验数据</h3>
            <table border="1" cellpadding="2" style="
						width: 100%;
						border-collapse: collapse;
						border: 2px solid #000;
					" class="ntable">
                <thead>
                    <tr>
                        <th colspan="2">按方程y=bx+a做线性回归分析</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>截距 a/nm</td>
                        <td><el-input v-model="model[11].value" class="w-50 m-2" @blur="getTime(11)" /></td>
                    </tr>
                    <tr>
                        <td>回归系数 b/nm</td>
                        <td><el-input v-model="model[12].value" class="w-50 m-2" @blur="getTime(12)" /></td>
                    </tr>
                    <tr>
                        <td>激光波长 λ=2|b|/nm</td>
                        <td><el-input v-model="model[13].value" class="w-50 m-2" @blur="getTime(13)" /></td>
                    </tr>
                </tbody>
            </table>
            <div>如果激光波长的真值为λ<sub>0</sub>=632.8mm,计算实验结果的绝对误差和相对误差。</div>
            <div>绝对误差δ=|λ-λ<sub>0</sub>|= <span class="f2"><el-input v-model="model[14].value" class="w-10 m-2" @blur="getTime(14)"/></span>nm,相对误差E<sub>r</sub>×100%=
                <span class="f2"><el-input v-model="model[15].value" class="w-10 m-2" @blur="getTime(15)" /></span>%</div>
            <n-divider title-placement="left">
                <h2>五、实验分析</h2>
            </n-divider>
            <div class="paper-analyze">
                <div style="margin-bottom: 10px; font-size: 17px">1.迈克尔干涉仪的测量精度是<span class="f2"><el-input v-model="model[16].value" class="w-10 m-2" @blur="getTime(16)" /></span>mm</div>
                <div style="margin-bottom: 10px; font-size: 17px">2.如果本次实验的数据的差距相对较大，主要原因可能是</div>
                <el-input type="textarea" v-model="model[17].value" style="height: 100px" @blur="getTime(17)"
                    resize='none' />
                    <div style="margin-bottom: 10px; font-size: 17px">3.调节微调手轮时，没看到圆环冒出或缩进，为什么？</div>
                <el-input type="textarea" v-model="model[18].value" style="height: 100px" @blur="getTime(18)"
                    resize='none' />
                    <div style="margin-bottom: 10px; font-size: 17px">4.越靠近等倾干涉圆环中心，条纹级次越
                        <span class="f2"><el-input v-model="model[19].value" class="w-10 m-2" @blur="getTime(19)" /></span>(填“高”或“低”)</div>
            </div>
            <div class="btn-group">
                <n-button type="info" @click="router.back()"> 保存并退出 </n-button>
                <router-link to="/">
                    <n-button type="success" @click="apply"> 提交 </n-button>
                </router-link>
            </div>
        </n-card>
    </div>
</template>

<style lang="scss" scoped>
.paper-title {
    .stu-info {
        display: flex;
        align-items: center;

        .stu-info-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 10px;
        }
    }
}

.ntable {
    margin: 10px;

    thead {
        background-color: #999;

        th {
            width: 100px;
            height: 50px;
            line-height: 50px;
            border: 2px solid black;
        }
    }

    tr {
        td {
            width: 100px;
            height: 50px;
            border: 2px solid black;
        }
    }
}

.paper-purpose {
    li {
        margin-top: 10px;
    }

}

.form1 {
    .form-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 10px;

        .input-group {
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%;

            .input {
                width: 100%;
            }
        }
    }
}
.f2 {
        display: inline-block;
        width: 100px;
        margin-right: 10px;
}
table {
    margin: 10px;

    tr {
        td {
            text-align: center;
            padding: 10px;

            input {
                width: 100%;
                height: 100%;
                //border: none;
                outline: none;
                text-align: center;
                box-sizing: border-box;

                &:focus {
                    border-radius: 5px;
                    border: 1px solid #000;
                }
            }
        }
    }
}

.btn-group {
    // 右下角
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    margin: 10px;

    .n-button {
        margin-left: 10px;
    }
}</style>